<?php
class Default_Model_Product extends Zend_Db_Table_Abstract
{
    protected $_name = 'product';
    public function getAllData($a=array()){
    	$query = $this->select()
                        ->setIntegrityCheck(false)
                        ->from(array('p'=>$this->_name))
                        ->joinLeft(array('i'=>'productitem'),'p.itemId =i.id', array('i.name as itemName'))
                        ->joinLeft(array('c'=>'productcat'),'i.catId =c.id', array('c.name as catName'))
                        ->joinLeft(array('a'=>'area'),'p.areaId = a.id', array('a.name as areaName'))
                        ->joinLeft(array('u'=>'user'),'p.userId = u.id', array('u.username','u.email'))
                        ->order('p.type desc');
        if(isset($a['view']) && $a['view']){
            $query->order('p.view desc');
        }else{
            $query->order('startDate desc');
        }                                   
        if(isset($a['wait'] ) && $a['wait']){
            $query->where('p.status=?', (int)$a['wait']);
        }          
        if(isset($a['title'] ) && $a['title']){
            $query->where('p.title like  "%'.$a['title'].'%"');
        }
        if(isset($a['itemId'] ) && $a['itemId']){
            $query->where('p.itemId=?', (int)$a['itemId']);
        }  
        if(isset($a['catId'] ) && $a['catId']){
            $query->where('c.id=?', (int)$a['catId']);
        }                                                                         
        if(isset($a['areaId'] ) && $a['areaId']){
            $query->where('p.areaId=?', (int)$a['areaId']);
        }
        if(isset($a['human'] ) && $a['human']){
            $query->where('p.human = "'.$a['human'].'"');
        }
        if(isset($a['type'] ) && $a['type']){
            $query->where('p.type = "'.$a['type'].'"');
        }
        if(isset($a['transaction'] ) && $a['transaction']){
            $query->where('p.transaction = "'.$a['transaction'].'"');
        }        
        if(isset($a['limit']) && $a['limit']){
            $limit = explode(',',$a['limit']);
            if(isset($limit[0]) && isset($limit[1]) && is_numeric($limit[0]) && is_numeric($limit[1])){
                $itemPerPage    = $limit[0];
                $offset         = $limit[1];
                $query->limit($itemPerPage,$offset);
            }
        }                
        $query->where('startDate < ?',(int)time());
        $query->where('endDate  > ?',(int)time());        
    	return $this->fetchAll($query);
    }  
    public function countData($a=array()){
        $query = $this->select()
                        ->setIntegrityCheck(false)
                        ->from(array('p'=>$this->_name),array('count(*) as Total'))
                        ->joinLeft(array('i'=>'productitem'),'p.itemId =i.id', array('i.name as itemName'))
                        ->joinLeft(array('c'=>'productcat'),'i.catId =c.id', array('c.name as catName'))
                        ->joinLeft(array('a'=>'area'),'p.areaId = a.id', array('a.name as areaName'))
                        ->joinLeft(array('u'=>'user'),'p.userId = u.id', array('u.username','u.email'))
                        ->order('p.type desc')->order('startDate desc');                           
        if(isset($a['wait'] ) && $a['wait']){
            $query->where('p.status=?', (int)$a['wait']);
        }          
        if(isset($a['title'] ) && $a['title']){
            $query->where('p.title like  "%'.$a['title'].'%"');
        }
        if(isset($a['itemId'] ) && $a['itemId']){
            $query->where('p.itemId=?', (int)$a['itemId']);
        }    
        if(isset($a['catId'] ) && $a['catId']){
            $query->where('c.id=?', (int)$a['catId']);
        }                                                                       
        if(isset($a['areaId'] ) && $a['areaId']){
            $query->where('p.areaId=?', (int)$a['areaId']);
        }
        if(isset($a['human'] ) && $a['human']){
            $query->where('p.human = "'.$a['human'].'"');
        }
        if(isset($a['type'] ) && $a['type']){
            $query->where('p.type = "'.$a['type'].'"');
        }
        if(isset($a['transaction'] ) && $a['transaction']){
            $query->where('p.transaction = "'.$a['transaction'].'"');
        }
        $query->where('startDate < ?',(int)time());
        $query->where('endDate  > ?',(int)time());        
    	$result= $this->fetchRow($query);
        return $result['Total'];
    }
    public function getAllDataMagage($a=array()){
        $query = $this->select()
                        ->setIntegrityCheck(false)
                        ->from(array('p'=>$this->_name))
                        ->joinLeft(array('i'=>'productitem'),'p.itemId =i.id', array('i.name as itemName'))
                        ->joinLeft(array('c'=>'productcat'),'i.catId =c.id', array('c.name as catName'))
                        ->joinLeft(array('a'=>'area'),'p.areaId = a.id', array('a.name as areaName'))
                        ->joinLeft(array('u'=>'user'),'p.userId = u.id', array('u.username','u.email'))
                        ->order('startDate desc');                                   
        $query->where('p.status != 0');                                                                                                                            
        if(isset($a['type'] ) && $a['type']){
            $query->where('p.type = "'.$a['type'].'"');
        }
        if(isset($a['userId'] ) && $a['userId']){
            $query->where('p.userId=?', (int)$a['userId']);
        }                 
    	return $this->fetchAll($query);
    }
    public function getDataByAreaId($id){
        $query = $this->select()
                        ->from(array('p'=>$this->_name))
                        ->where('areaId = ?',(int)$id);            	
        return $this->fetchAll($query);
    }    
    public function getDataById($id){
        $query = $this->select()
                        ->setIntegrityCheck(false)
                        ->from(array('p'=>$this->_name))
                        ->joinLeft(array('i'=>'productitem'),'p.itemId =i.id', array('i.name as itemName'))
                        ->joinLeft(array('c'=>'productcat'),'i.catId =c.id', array('c.name as catName'))
                        ->joinLeft(array('a'=>'area'),'p.areaId = a.id', array('a.name as areaName'))
                        ->joinLeft(array('u'=>'user'),'p.userId = u.id', array('u.username','u.email'))
                        ->where('p.id = ?',(int)$id);                                    	
        return $this->fetchRow($query);
    } 
    public function getDataNewest($limit){
        $query = $this->select()
                        ->setIntegrityCheck(false)
                        ->from(array('p'=>$this->_name))
                        ->joinLeft(array('i'=>'productitem'),'p.itemId =i.id', array('i.name as itemName'))
                        ->joinLeft(array('c'=>'productcat'),'i.catId =c.id', array('c.name as catName'))
                        ->joinLeft(array('a'=>'area'),'p.areaId = a.id', array('a.name as areaName'))
                        ->joinLeft(array('u'=>'user'),'p.userId = u.id', array('u.username','u.email'))
                        ->where('p.status = 2')
                        ->where('startDate < ?',(int)time())
                        ->where('endDate  > ?',(int)time())                        
                        ->order('startDate desc')
                        ->limit($limit);                                                            	
        return $this->fetchAll($query);
    }
     
    public function addData($data){
        $data=array('itemId'        =>$data['itemId'],
                    'userId'        =>$data['userId'],
                    'areaId'        =>$data['areaId'],
                    'title'         =>$data['title'],
                    'shortTitle'    =>$data['shortTitle'],
                    'photo'         =>$data['photo'],
                    'type'          =>$data['type'],
                    'price'         =>$data['price'],
                    'content'       =>$data['content'],
                    'contact'       =>$data['contact'],
                    'mobile'        =>$data['mobile'],
                    'emailCont'     =>$data['emailCont'],
                    'addressCont'   =>$data['addressCont'],
                    'status'        =>$data['status'],                    
                    'transaction'   =>$data['transaction'],
                    'human'         =>$data['human'],
                    'createDate'    =>$data['createDate'],
                    'startDate'     =>$data['startDate'],                                                            
                    'endDate'       =>$data['endDate'],
                    'priceAd'       =>$data['priceAd']
                    );                    
        $id=$this->insert($data, $this->_name);
        return $id;
    }
    public function updateAll($id, $data=array()){
        return $this->update($data, 'id = '.(int)$id);
    }
    public function updateData($id = '',$data){        
        $data=array('itemId'        =>$data['itemId'],
                    'userId'        =>$data['userId'],
                    'areaId'        =>$data['areaId'],
                    'title'         =>$data['title'],
                    'shortTitle'    =>$data['shortTitle'],
                    'photo'         =>$data['photo'],
                    'price'         =>$data['price'],
                    'content'       =>$data['content'],
                    'contact'       =>$data['contact'],
                    'mobile'        =>$data['mobile'],
                    'emailCont'     =>$data['emailCont'],
                    'addressCont'   =>$data['addressCont'],
                    'status'        =>$data['status'],                    
                    'transaction'   =>$data['transaction'],
                    'human'         =>$data['human'],
                    'createDate'    =>$data['createDate'],
                    'startDate'     =>$data['startDate'],                    
                    'endDate'       =>$data['endDate'],
                    'priceAd'       =>$data['priceAd'],
                    'type'          =>$data['type']);                            
        return $this->update($data, 'id = '.(int)$id);
    }
    public function updateWait($id, $data=array()){
        return $this->update($data, 'id = '.(int)$id);
    }
    public function deleteData($id){        
        $this->delete('id = '.(int)$id);
    } 
             
}